/*
 * PhotoSetDAO.java
 *
 * Created on March 22, 2007, 1:45 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package edu.byu.isys413.group1E.data;
import edu.byu.isys413.group1E.handlers.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

/**
 * Handles DB interaction for PhotoSetBOs
 */
public class PhotoSetDAO extends DAObject {
    
    /**
     * contains an instance of PhotoSetDAO
     */
    private static PhotoSetDAO instance = null;

    /**
     * Constructor for PhotoSetDAO
     */
    public PhotoSetDAO() {
    }
    
    /**
     * Gets an Instance of a PhotosetDAO
     * @return Returns the appropriate object or stringReturns a Photoset
     */
    public static synchronized PhotoSetDAO getInstance() {
        if (instance == null) {
            instance = new PhotoSetDAO();
        }
        return instance;
    }

    /**
     * Creates a PhotoSet
     * @return Returns the appropriate object or stringReturns PsBO     
     * @throws java.lang.Exception Handles other exceptions
     */
    public BusinessObject create() throws Exception{
        System.out.println("CREATING PHOTOSET");
        String id = GUID.generate();
        BusinessObject bo = new PhotoSetBO(id);
        Cache c = Cache.getInstance();
        c.put(bo.getId(), bo);
        return bo;
    }
        
    /**
     * Reads a PhotoSet
     * @param id Reads the GUID
     * @param conn Connection Pool
     * @return Returns the appropriate object or stringReturns a PhotoSet
     * @throws java.sql.SQLException Handles any error related to SQL queriesErrors are Thrown
     * @throws edu.byu.isys413.group1E.handlers.DataException Handles Data Exceptions Errors are Thrown
     */
    public BusinessObject read(String id, Connection conn) throws SQLException, DataException {
        // check to see if id in the cache
        Cache c = Cache.getInstance();
        PhotoSetBO bo = (PhotoSetBO)c.get(id);
        if (bo == null) {
            // if not in the cache, get a result set from
            Statement stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery("SELECT * FROM PHOTO_SET WHERE PHOTOSETID = '" + id + "'");
            conn.commit();
            
            //build the object from the result set
            if (rs.next()) {
                
                bo = new PhotoSetBO     (rs.getString("PHOTOSETID"));

                //BO POPULATION
                bo.setNuminSet              (rs.getInt          ("NUMBERINSET"));
                bo.setDescription           (rs.getString     ("DESCRIPTION"));

                
                bo.setAlreadyInDB(true);
                bo.setIsDirty(false);
                stmt.close();
            }
        }
        // return the BO
        return bo;
    }
        
    /**
     * Reads all Photosets
     * 
     * 
     * @return Returns the appropriate object or string
     * @throws edu.byu.isys413.group1E.handlers.DataException handles any errors from data
     * @throws java.sql.SQLException catches any errors from the SQL execution
     * @throws edu.byu.isys413.group1E.handlers.ConnectionPoolException catches any errors with the connection
     */
    public  ArrayList<BusinessObject> readAll() throws DataException, SQLException, ConnectionPoolException {

        ArrayList<BusinessObject> boList = new ArrayList<BusinessObject>();
        //now build the query
        String query = "SELECT * FROM PHOTO_SET";
        
        Connection conn = ConnectionPool.getInstance().get();
        Statement stmt = conn.createStatement();
        ResultSet rs = stmt.executeQuery(query);
        conn.commit();
        
        //extract the elements from the result set and build a list of results
        //build the object from the result set
        int i = 0;
        while (rs.next()) {
            PhotoSetBO bo = new PhotoSetBO(rs.getString("PHOTOSETID"));

            
                //BO POPULATION
                bo.setNuminSet              (rs.getInt          ("NUMBERINSET"));
                bo.setDescription           (rs.getString     ("DESCRIPTION"));

            
            bo.setAlreadyInDB(true);
            bo.setIsDirty(false);
            boList.add(i, bo);
            i++;

        }//while
        
        stmt.close();
        ConnectionPool.getInstance().release(conn);
        return boList;
    }
    
    /**
     * Updates Photoset parameters
     * 
     * 
     * @param bo contains the object to be updated
     * @param conn contains a connection
     * @throws edu.byu.isys413.group1E.handlers.DataException handles any errors from data
     * @throws java.sql.SQLException catches any errors from the SQL execution
     */
    public void update(BusinessObject bo, Connection conn) throws SQLException, DataException {
          //update the DB
        PhotoSetBO BO = (PhotoSetBO)bo;
        PreparedStatement pstmt = conn.prepareStatement("UPDATE PHOTO_SET  SET NUMBERINSET = ?, DESCRIPTION = ?  WHERE PHOTOSETID = ?");
                
        
        //CUSTOM UPDATE STUFF
        pstmt.setInt          (1, BO.getNuminSet());
        pstmt.setString     (2, BO.getDescription());
        pstmt.setString     (3, BO.getId());
        
        pstmt.executeUpdate();
        pstmt.close();
        bo.setAlreadyInDB(true);
    }

    /**
     * Inserts a new Photoset
     * 
     * @param bo takes a BO
     * @param conn contains the connection
     * @throws edu.byu.isys413.group1E.handlers.DataException handles any errors from data
     * @throws java.sql.SQLException catches any errors from the SQL execution
     */
    public void insert(BusinessObject bo, Connection conn) throws SQLException, DataException {
        
                 //update the DB
         PhotoSetBO BO = (PhotoSetBO)bo;
         PreparedStatement pstmt = conn.prepareStatement("INSERT INTO PHOTO_SET (PHOTOSETID, NUMBERINSET, DESCRIPTION)  VALUES ( ?, ?, ?)");
         
         //GETTER
        pstmt.setString     (1, BO.getId());
        pstmt.setInt          (2, BO.getNuminSet());
        pstmt.setString     (3, BO.getDescription());
        //System.out.println(pstmt.toString());
         pstmt.executeUpdate();
         
         pstmt.close();
         // tell the object that it's now in the db (so we call update next time not insert)
         bo.setAlreadyInDB(true);
         bo.clean();
    }

}
